<?php

namespace Think;

class AjaxPage {

    private $total;      //总记录  
    private $pagesize;    //每页显示多少条  
    private $limit;          //limit  
    private $page;           //当前页码  
    private $pagenum;      //总页码  
    private $url;           //地址  
    private $bothnum;      //两边保持数字分页的量 
    public $rollPage   = 11;// 分页栏每页显示的页数
    private $mysql;
    private $table;
    private $lastSuffix = true; // 最后一页是否显示总页数

    
    // 分页显示定制
    private $config  = array(
        'header' => '<span class="rows">共 %TOTAL_ROW% 条记录</span>',
        'prev'   => '<<',
        'next'   => '>>',
        'first'  => '1...',
        'last'   => '...%TOTAL_PAGE%',
        'theme'  => '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%',
    );
    
    //构造方法初始化  
    public function __construct($_total, $_pagesize,$parameter = array()) {
        $this->total = $_total ? $_total : 1;
        $this->pagesize = $_pagesize;
        $this->parameter  = empty($parameter) ? $_GET : $parameter;
        $this->pagenum = ceil($this->total / $this->pagesize);
        $this->limit = "LIMIT " . ($this->page - 1) * $this->pagesize . ",$this->pagesize";
        $this->url = $this->setUrl();
        $this->bothnum = 2;
        $this->page    = empty($_GET['p']) ? 1 : intval($_GET['p']);
    }

    //拦截器  
    public function __get($_key) {
        return $this->$_key;
    }
    
    /**
     * 定制分页链接设置
     * @param string $name  设置名称
     * @param string $value 设置值
     */
    public function setConfig($name,$value) {
        if(isset($this->config[$name])) {
            $this->config[$name] = $value;
        }
    }

    //获取当前页码  
    public function setPage() {
        if (!empty($_GET['page'])) {
            if ($_GET['page'] > 0) {
                if ($_GET['page'] > $this->pagenum) {
                    return $this->pagenum;
                } else {
                    return $_GET['page'];
                }
            } else {
                return 1;
            }
        } else {
            return 1;
        }
    }

    //获取地址  
    private function setUrl() {
        $_url = $_SERVER["REQUEST_URI"];
        $_par = parse_url($_url);
        if (isset($_par['query'])) {
            parse_str($_par['query'], $_query);
            unset($_query['page']);
            $_url = $_par['path'] . '?' . http_build_query($_query);
        }
        return $_url . "?";
    }

    private function pageList() {
        $_pagelist = '';
        for ($i = $this->bothnum; $i >= 1; $i--) {
            $_page = $this->page - $i;
            if ($_page < 1)
                continue;
            $_pagelist .= ' <a href="' . $this->url . '&page=' . $_page . '">' . $_page . '</a> ';
        }
        $_pagelist .= ' <span class="me">' . $this->page . '</span> ';
        for ($i = 1; $i <= $this->bothnum; $i++) {
            $_page = $this->page + $i;
            if ($_page > $this->pagenum)
                break;
            $_pagelist .= ' <a href="' . $this->url . '&page=' . $_page . '">' . $_page . '</a> ';
        }
        return $_pagelist;
    }

    public function pageList_ajax($action) {
        if(0 == $this->total) return '';
        /* 生成点击 */
        $this->parameter[$this->p] = $this->p;
        
        /* 计算分页信息 */
        $this->pagenum = ceil($this->total / $this->pagesize); //总页数
        if(!empty($this->pagenum) && $this->page > $this->pagenum) {
            $this->page = $this->pagenum;
        }
        /* 计算分页临时变量 */
        $now_cool_page      = $this->rollPage/2;
        $now_cool_page_ceil = ceil($now_cool_page);
        $this->lastSuffix && $this->config['last'] = $this->pagenum;
        
        
        //上一页
        $up_row  = $this->page - 1;
        if($action == 'searchDiary'){
            $up_page = $up_row > 0 ? '<a class="pointer prev" onclick="searchDiary('.$up_row.',\''.$this->parameter['user_id'].'\',\''.$this->parameter['user_name'].'\','.$this->parameter['is_view'].')">' . $this->config['prev'] . '</a>' : '';
        }
        
        //下一页
        $down_row  = $this->page + 1;
        $down_page = ($down_row <= $this->pagenum) ? '<a class="pointer next" onclick="searchDiary('.$down_row.',\''.$this->parameter['user_id'].'\',\''.$this->parameter['user_name'].'\','.$this->parameter['is_view'].')">' . $this->config['next'] . '</a>' : '';

        //第一页
        $the_first = '';
        if($this->pagenum > $this->rollPage && ($this->page - $now_cool_page) >= 1){
            if($action == 'searchDiary'){
                $the_first = '<a class="pointer first" onclick="searchDiary(1,\''.$this->parameter['user_id'].'\',\''.$this->parameter['user_name'].'\','.$this->parameter['is_view'].')">' . $this->config['first'] . '</a>';
            }  
                
        }

        //最后一页
        $the_end = '';
        if($this->pagenum > $this->rollPage && ($this->page + $now_cool_page) < $this->pagenum){
            if($action == 'searchDiary'){
                $the_end = '<p style="text-align:center;margin-top:1rem;">共<a class="pointer last" onclick="searchDiary('.$this->pagenum.',\''.$this->parameter['user_id'].'\',\''.$this->parameter['user_name'].'\','.$this->parameter['is_view'].')">' . $this->config['last'] . '</a>页</p>';
            }
                
        }
        //数字连接
        $link_page = "";
        for($i = 1; $i <= $this->rollPage; $i++){
            if(($this->page - $now_cool_page) <= 0 ){
                $page = $i;
            }elseif(($this->page + $now_cool_page - 1) >= $this->pagenum){
                $page = $this->pagenum - $this->rollPage + $i;
            }else{
                $page = $this->page - $now_cool_page_ceil + $i;
            }
            if($action == 'searchDiary'){
                $this->click = 'onclick = "searchDiary('.$page.',\''.$this->parameter['user_id'].'\',\''.$this->parameter['user_name'].'\','.$this->parameter['is_view'].')"';
            }
            if($page > 0 && $page != $this->page){

                if($page <= $this->pagenum){
                    $link_page .= '<a class="pointer num aaa" '.$this->click.'>' . $page . '</a>';
                }else{
                    break;
                }
            }else{
                if($page > 0 && $this->pagenum != 1){
                    $link_page .= '<span class="pointer current aaa">' . $page . '</span>';
                }
            }
        }
        //替换分页内容
        $page_str = str_replace(
            array('%HEADER%', '%NOW_PAGE%', '%UP_PAGE%', '%DOWN_PAGE%', '%FIRST%', '%LINK_PAGE%', '%END%', '%TOTAL_ROW%', '%TOTAL_PAGE%'),
            array($this->config['header'], $this->page, $up_page, $down_page, $the_first, $link_page, $the_end, $this->total, $this->pagenum),
            $this->config['theme']);
        return "<span>{$page_str}</span>";
    }

}

?>    